package com.heima.exception;


import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//controller增强注解统一异常处理
@Configuration
@RestControllerAdvice
@Slf4j
public class ExceptionCatch {
    /**
     * 不可预知的异常捕获
     * @param e
     * @return
     */

    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandle(Exception e){
        log.error("服务器出现异常==>{}",e);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器出现异常");
    }

    /**
     * 自定义异常的捕获
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult customHandle(Exception e){
        log.error("服务器出现异常==>{}",e);

        CustomException customException = (CustomException) e;

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器出现异常");
    }
}
